package DMSXL._2024.BinaryTrees;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class pathSum {
    public static void main(String[] args) {

    }
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null){
            return result;
        }
        path.add(root.val);
        backTrack(root,targetSum - root.val);
        return result;
    }

    public void backTrack(TreeNode root, int targetSum){
        if(root.left == null && root.right == null && targetSum == 0){
            result.add(new ArrayList<>(path));
        }
        if(root.left != null){
            path.add(root.left.val);
            backTrack(root.left,targetSum - root.left.val);
            path.removeLast();
        }
        if(root.right != null){
            path.add(root.right.val);
            backTrack(root.right,targetSum - root.right.val);
            path.removeLast();
        }
    }
}
